#pragma once
#include <iostream>
#include <string>
#include <unistd.h>

#include "Init.hpp"

Init init;

class Task
{
public:
    Task(int sockfd, const std::string &clientip, const uint16_t clientport)
        :_sockfd(sockfd), _clientip(clientip), _clientport(clientport)
    {}

    void run()
    {
       char buffer[4096];
        while (true)
        {
            ssize_t n = read(_sockfd, buffer, sizeof(buffer) - 1);
            if (n > 0)
            {
                buffer[n] = 0;
                std::cout << "client key# " << buffer << std::endl;
                std::string echo_string = init.translation(buffer);
                std::cout << echo_string << std::endl;
                write(_sockfd, echo_string.c_str(), echo_string.size());
            }
            else if (n == 0)
            {
                std::cout << "Server Quit...." << std::endl;
                break;
            }
        }
    }
    void operator ()()
    {
        run();
    }
    
    ~Task()
    {
    }

private:
    int _sockfd;
    std::string _clientip;
    uint16_t _clientport;
};